﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCCN.DAL;

namespace SCCN.BLL
{
    public class CategoryBLL
    {
        private static CategoryBLL instance = null;

        /// <summary>
        /// 单例构造
        /// </summary>
        /// <returns></returns>
        public static CategoryBLL getInstance()
        {
            if (instance == null)
            {
                instance = new CategoryBLL();
            }
            return instance;
        }

        /// <summary>
        /// 新增资讯分类
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddCategory(SC_Category obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                entities.AddToSC_Category(obj);
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 删除指定分类
        /// </summary>
        /// <param name="id">分类Id</param>
        /// <returns></returns>
        public bool DeleteCategory(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_Category obj = entities.SC_Category.FirstOrDefault(r => r.Id == id);
                obj.IsDelete = true;
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 更新资讯分类
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdateCategory(SC_Category obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_Category objCatetgory = entities.SC_Category.FirstOrDefault(v => v.Id == obj.Id);
                entities.Detach(objCatetgory);
                objCatetgory.CategoryName = obj.CategoryName;
                objCatetgory.IsDelete = obj.IsDelete;
                entities.Attach(objCatetgory);
                var newCategory = entities.ObjectStateManager.GetObjectStateEntry(objCatetgory);
                newCategory.SetModified();
                newCategory.SetModifiedProperty("CategoryName");
                newCategory.SetModifiedProperty("IsDelete");
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 查询所有分类
        /// </summary>
        /// <returns></returns>
        public List<SC_Category> GetCategory()
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Category.Where(r => r.IsDelete == false).ToList<SC_Category>();
            }
        }

        /// <summary>
        /// 查询所有分类
        /// </summary>
        /// <returns></returns>
        public List<SC_Category> GetCategoryByCondition(SC_Category obj, int pageIndex, int pageSize, out int rowCount)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                var list = from r in entities.SC_Category.Where(a => a.IsDelete == false)
                           select r;
                if (obj.CategoryName.Length > 0)
                {
                    list = list.Where(a => a.CategoryName.Contains(obj.CategoryName));
                }
                rowCount = list.Count<SC_Category>();
                if (pageSize > 0)
                {
                    list = list.OrderBy(a => a.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                }
                return list.ToList<SC_Category>();
            }
        }

        /// <summary>
        /// 根据分类名查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public List<SC_Category> GetCategorysByName(string categoryName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Category.Where(r => r.IsDelete == false && r.CategoryName.Contains(categoryName)).ToList<SC_Category>();
            }
        }

        /// <summary>
        /// 根据分类名查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public SC_Category GetCategoryByName(string categoryName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Category.FirstOrDefault(r => r.IsDelete == false && r.CategoryName == categoryName);
            }
        }

        /// <summary>
        /// 根据ID查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SC_Category GetCategoryByID(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_Category.FirstOrDefault(r => r.IsDelete == false && r.Id == id);
            }
        }

    }
}
